//
//  AuthenViewController.m
//  HxgProject
//
//  Created by hxg on 2018/3/1.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "AuthenViewController.h"
#import "AuthenTableViewCell.h"

#import "PersonInfoAuthenVC.h"
#import "ContectInfoVC.h"
#import "ReputationInfoVC.h"

#import "AuthenService.h"
#import "ContactInfo.h"
#import "ReputationDetail.h"
#import "PersonInfo.h"

#import "HxgTools.h"
#import "AuthenInfo.h"
#import "BankAuthenVC.h"
#import "CompanyAuthenVC.h"

#import "BankInfo.h"
#import "CompanyInfo.h"

#import "RootTabViewController.h"

@interface AuthenViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) UITableView *autherTableView;


// 认证对象数组
@property (nonatomic, strong) NSMutableArray *autherInfoArrs;


// 认证对象
@property (nonatomic, strong) AuthenService *authenService;

// 存储个人信息的全局变量
@property (nonatomic, strong) PersonInfo *globalPerson;

// 存储个人联系人信息
@property (nonatomic, strong) ContactInfo *globalContact;

// 存储个人信誉信息
@property (nonatomic, strong) ReputationDetail *globalXinYu;

// 单位信息
@property (nonatomic, strong) CompanyInfo *globalCompanyInfo;

// 银行信息
@property (nonatomic, strong) BankInfo *globalBankInfo;


@end

@implementation AuthenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setTitleTextAttributes:
  @{NSFontAttributeName:mainFont,
    NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#FFFFFF"]}];
    
    self.view.backgroundColor = [UIColor whiteColor];

    [self setUpUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 查询联系人认证
    [self getContactAuthenInfo];
    
    // 查询个人认证信息
    [self getPersonAuthenInfo];

    // 查询信誉认证
    [self getXinYuInfo];
    
    // 查询单位信息
    [self getCompanAuthenInfo];
    
    // 查询银行信息
    [self getBankInfo];
}

#pragma mark - ###getActions###
- (void)getPersonAuthenInfo
{
    __weak AuthenViewController *weakSelf = self;
    [self.authenService getUserInfoWithCallBack:^(PersonInfo *person) {
        if (!isEmptyString(person.name)
            && !isEmptyString(person.idCard)
            && !isEmptyString(person.qq)
            && !isEmptyString(person.webChat)
            && !isEmptyString(person.chsiAccount)
            && !isEmptyString(person.chsiPwd)
            && !isEmptyString(person.operatorPwd)) {
            weakSelf.globalPerson = person;
            [[NSUserDefaults standardUserDefaults] setBool:YES
                                                    forKey:kPersonInfoAuthenRes];
        } else  {
            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kPersonInfoAuthenRes];
        }
        [weakSelf.autherTableView reloadData];
    }];
}

- (void)getXinYuInfo
{
    __weak AuthenViewController *weakSelf = self;
    [self.authenService getReputationDetailWithCallBack:^(ReputationDetail *rep) {
        if (!isEmptyString(rep.zfbAccount)
            && !isEmptyString(rep.tbAccount)
            )
        {
            weakSelf.globalXinYu = rep;
            [[NSUserDefaults standardUserDefaults] setBool:YES
                                                    forKey:kXinYuAuthenRes];
        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kXinYuAuthenRes];
        }
        [weakSelf.autherTableView reloadData];
    }];
}

- (void)getContactAuthenInfo
{
    __weak AuthenViewController *weakSelf = self;
    [self.authenService getContactsWithCallBack:^(ContactInfo *info) {
        if (!isEmptyString(info.contactName1)
            && !isEmptyString(info.contactName2))
        {
            weakSelf.globalContact = info;
            [[NSUserDefaults standardUserDefaults] setBool:YES
                                                    forKey:kContactAuthenRes];
        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kContactAuthenRes];
        }
        [weakSelf.autherTableView reloadData];
    }];    
}

- (void)getCompanAuthenInfo{
    __weak AuthenViewController *weakSelf = self;
    [self.authenService getCompanyInfoWithCallBack:^(CompanyInfo *info) {
        if (!isEmptyString(info.companyName))
        {
            weakSelf.globalCompanyInfo = info;
            [[NSUserDefaults standardUserDefaults] setBool:YES
                                                    forKey:kCompanyAuthenRes];
        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kCompanyAuthenRes];
        }
        [weakSelf.autherTableView reloadData];
        
    } ];
}

- (void)getBankInfo{
    __weak AuthenViewController *weakSelf = self;
    [self.authenService getBankInfoWithCallBack:^(BankInfo *info) {
        if (!isEmptyString(info.bank))
        {
            weakSelf.globalBankInfo = info;
            [[NSUserDefaults standardUserDefaults] setBool:YES
                                                    forKey:kBankInfoAuthenRes];
        } else {
            [[NSUserDefaults standardUserDefaults] setBool:NO
                                                    forKey:kBankInfoAuthenRes];
        }
        [weakSelf.autherTableView reloadData];
    }];
}


#pragma mark  ###UI###
- (void)setUpUI
{
    self.navigationItem.title = @"认证";
    
    self.navigationController.navigationBar.barTintColor = kNavagationBarBgColor;
    self.view.backgroundColor = defaultViewBgColor;
    [self.view addSubview:self.autherTableView];
    
    UITabBarController *tabBarVC = [[UITabBarController alloc] init];
    CGFloat tabBarHeight = tabBarVC.tabBar.frame.size.height;
    
    self.autherTableView.backgroundColor = defaultViewBgColor;
    NSLog(@"tabBarHeight:%f",tabBarHeight);
    
    [self.autherTableView makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        }else{
            make.top.equalTo(self.view.mas_top);
        }
        make.left.equalTo(self.view.mas_left);
        make.width.equalTo(self.view.frame.size.width);
        make.bottom.equalTo(self.view.mas_bottom).offset(-tabBarHeight);
//        make.bottom.equalTo(self.view.mas_bottom);
//        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width, kDefaultCellHeight * 5.0));
    }];
    
    [self.autherTableView registerClass:[AuthenTableViewCell class]
                 forCellReuseIdentifier:@"cell"];
}

-(UITableView *)autherTableView
{
    if (!_autherTableView)
    {
        _autherTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreen_Width, kScreen_Height) style:UITableViewStylePlain];
        _autherTableView.delegate = self;
        _autherTableView.dataSource = self;
        _autherTableView.tableFooterView = [[UIView alloc] init];
    }
    
    return _autherTableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableViewDelegate

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.autherInfoArrs.count;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    AuthenInfo *cell_authenInfo = self.autherInfoArrs[indexPath.row];
    
    static NSString *identifier = @"cell";
    
    AuthenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell)
    {
        cell = [[AuthenTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.desLbl.text = cell_authenInfo.authenDes;
    cell.personIcon.image = [UIImage imageNamed:cell_authenInfo.authenIcon];
    cell.titleNameLbl.text = cell_authenInfo.authenName;
    
    if (0 == indexPath.row)
    {
        [self getAutherRes:cell withAuthenName:kPersonInfoAuthenRes];
    }
    else if (1 == indexPath.row)
    {
        [self getAutherRes:cell withAuthenName:kBankInfoAuthenRes];
    } else if (2 == indexPath.row) {
        [self getAutherRes:cell withAuthenName:kCompanyAuthenRes];
    } else if (3 == indexPath.row) {
        [self getAutherRes:cell withAuthenName:kContactAuthenRes];
    } else if (4 == indexPath.row) {
        [self getAutherRes:cell withAuthenName:kXinYuAuthenRes];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kDefaultCellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        PersonInfoAuthenVC *personVC = [[PersonInfoAuthenVC alloc] init];
        personVC.showPerson = self.globalPerson;
        [self.navigationController pushViewController:personVC animated:YES];
    } else if (indexPath.row == 1) {
        BankAuthenVC *bankVC = [[BankAuthenVC alloc] init];
        bankVC.bankInfo = self.globalBankInfo;
        [self.navigationController pushViewController:bankVC animated:YES];
    } else if (indexPath.row == 2) {
        CompanyAuthenVC *companyVC = [[CompanyAuthenVC alloc] init];
        companyVC.companyInfo = self.globalCompanyInfo;
        [self.navigationController pushViewController:companyVC animated:YES];
    } else if (indexPath.row == 3) {
        ContectInfoVC *personVC = [[ContectInfoVC alloc] init];
        personVC.showContact = self.globalContact;
        [self.navigationController pushViewController:personVC animated:YES];
    }
    else
    {
        ReputationInfoVC *personVC = [[ReputationInfoVC alloc] init];
        personVC.showReputationInfo = self.globalXinYu;
        [self.navigationController pushViewController:personVC animated:YES];
    }
}

#pragma mark - ###### showAutherRes <##>######

- (void)getAutherRes:(AuthenTableViewCell *)cell withAuthenName:(NSString *)authenName{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:authenName])
    {
        cell.authenLbl.text = @"已认证";
        cell.authenLbl.textColor = [UIColor colorWithHexString:@"389a56"];
    }
    else
    {
        cell.authenLbl.text = @"未认证";
        cell.authenLbl.textColor = [UIColor colorWithHexString:@"ef3d07"];
    }
}


#pragma mark getter、setter
- (NSMutableArray *)autherInfoArrs
{
    if (!_autherInfoArrs) {
        _autherInfoArrs = [[NSMutableArray alloc] init];
        
        NSMutableArray *autheDicrArrs = [[HxgTools sharedHxgTools] getProprotiesFormPlistFile:@"AuthorArr"];
        
        for (NSDictionary *dic in autheDicrArrs) {
            AuthenInfo *authenInfo = [[AuthenInfo alloc] initWithDic:dic];
            [_autherInfoArrs addObject:authenInfo];
        }
    }
    return _autherInfoArrs;
}


- (AuthenService *)authenService
{
    if (!_authenService)
    {
        _authenService = [[AuthenService alloc] init];
    }
    return _authenService;
}

-(PersonInfo *)globalPerson
{
    if (!_globalPerson) {
        _globalPerson = [[PersonInfo alloc] init];
    }
    return _globalPerson;
}

- (ContactInfo *)globalContact
{
    if (!_globalContact)
    {
        _globalContact = [[ContactInfo alloc] init];
    }
    return _globalContact;
}

- (ReputationDetail *)globalXinYu
{
    if (!_globalXinYu)
    {
        _globalXinYu = [[ReputationDetail alloc] init];
    }
    return _globalXinYu;
}

@end
